import type { Response } from "@/service/api/types/request";
import type { MockAdapterInstance } from "../index";
import type {
    Reservation,
    Order,
    Requirement,
    Cancellation,
} from "@/service/api/types/reservation";

import _ from "lodash";

const generateReservation = () => {
    const n = orders.length;
    if (n === 0) {
        return [];
    }
    const reservations: Reservation[] = [];
    for (let i = 0; i < n; i++) {
        const status = _.sample([
            "verifying",
            "rejected",
            "confirmed",
            "refunded",
            "completed",
        ]);
        if (["unpaid", "expired"].includes(orders[i].payment_status)) continue;
        const reservation: Reservation = {
            id: _.uniqueId(),
            orderId: orders[i].id!,
            status: status as
                | "verifying"
                | "rejected"
                | "confirmed"
                | "refunded"
                | "completed",
            processedTime: status === "verifying" ? null : Date.now(),
            createdTime: orders[i].paymentTime!,
        };
        reservations.push(reservation);
    }
    return reservations;
};

const generateOrder = (n: number) => {
    const oneMonthAgo = new Date();
    oneMonthAgo.setMonth(new Date().getMonth() - 1);
    return _.times(n, () => {
        const paymentStatus = _.sample(["unpaid", "paid", "expired"]);
        const order: Order = {
            id: _.uniqueId(),
            customerId: "114514191",
            roomId: _.uniqueId(),
            requirement: {
                reserveDate:
                    Date.now() + _.random(-5, 30) * 24 * 60 * 60 * 1000,
                startTime: _.random(8, 20),
                endTime: _.random(8, 20),
                area: _.random(100, 1000),
                capacity: _.random(10, 100),
                roomType: _.sample(["classroom", "roundtable"]),
                device: {
                    speaker: _.random(0, 1) === 0,
                    internet: _.random(0, 1) === 0,
                    projector: _.random(0, 1) === 0,
                },
                notes: "这是一个测试订单",
            },
            amount: _.random(1000, 10000),
            payment_status: paymentStatus as "unpaid" | "paid" | "expired",
            paymentTime:
                paymentStatus === "paid"
                    ? _.random(oneMonthAgo.getTime(), Date.now())
                    : null,
            createdTime: _.random(oneMonthAgo.getTime(), Date.now()),
        };
        return order;
    });
};

const orders = generateOrder(25);
const reservations = generateReservation();

export default function reservationMock(mock: MockAdapterInstance) {
    mock.onPost("/order/submit").reply((config) => {
        const response: Response<null> = {} as Response<null>;
        const order: Order = JSON.parse(config.data);

        response.code = 1;
        response.data = null;

        return [200, response];
    });

    mock.onPost("/order/get/unpaid").reply((config) => {
        const response: Response<Order> = {} as Response<Order>;
        const { customerId } = JSON.parse(config.data);

        const order: Order = {
            id: "order_1",
            customerId,
            roomId: "room_1",
            requirement: {
                reserveDate: new Date("2025/05/11").getTime(),
                startTime: 18,
                endTime: 20,
                area: 100,
                capacity: 271,
                roomType: "classroom",
                device: {
                    speaker: true,
                    internet: true,
                    projector: true,
                },
                notes: "这是一个测试订单",
            },
            amount: 1000,
            payment_status: "unpaid",
            paymentTime: null,
            createdTime: Date.now(),
        };

        response.code = 1;
        response.data = order;

        return [200, response];
    });

    mock.onPost("/order/pay").reply((config) => {
        const response: Response<null> = {} as Response<null>;
        const { reservation } = JSON.parse(config.data);
        response.code = 1;
        response.data = null;

        return [200, response];
    });

    mock.onPost("/order/cancel").reply((config) => {
        const response: Response<null> = {} as Response<null>;
        const { orderId } = JSON.parse(config.data);

        response.code = 1;
        response.data = null;

        return [200, response];
    });

    mock.onPost("/order/get/id").reply((config) => {
        const response: Response<Order | null> = {} as Response<Order | null>;
        const { orderId } = JSON.parse(config.data);

        const order = orders.find((order) => order.id === orderId);
        response.code = 1;
        if (!order) {
            response.data = null;
        } else {
            response.data = order;
        }
        return [200, response];
    });

    mock.onPost("/reservation/get/list").reply((config) => {
        const response: Response<Reservation[]> = {} as Response<Reservation[]>;
        const { customerId, page, size } = JSON.parse(config.data);

        if ((page - 1) * size > reservations.length) {
            response.code = 1;
            response.data = [];
            return [200, response];
        }

        response.code = 1;
        response.data = reservations.slice((page - 1) * size, page * size);

        return [200, response];
    });

    mock.onPost("/reservation/cancel").reply((config) => {
        const response: Response<null> = {} as Response<null>;
        const { cancellation } = JSON.parse(config.data);

        response.code = 1;
        response.data = null;

        return [200, response];
    });
}
